package com.spring.boot.sample.test2.config;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;

/**
 * Druid
 * 配置监控统计功能
 * 两种方式：
 * ①原生servlet，filter方式：启动类加上注解：@ServletComponentScan是的spring能够扫描到我们自己编写的servlet和filter。
 * ②使用代码注册Servlet
 * 实例使用②
* <p>Title:DruidConfiguration </p>
* <p>Description: </p>
* <p>Company: </p> 
* @author kexl
* @date 下午2:54:51
 */

@Configuration
public class DruidConfiguration {

	//servlet 代码
	/**
	 * druid数据源状态监控.
	 * @author Administrator
	 *
	 */
	 
	/*@WebServlet(urlPatterns="/druid/*",
	           initParams={
	                   @WebInitParam(name="allow",value="192.168.1.72,127.0.0.1"),// IP白名单 (没有配置或者为空，则允许所有访问)
	                    @WebInitParam(name="deny",value="192.168.1.73"),// IP黑名单 (存在共同时，deny优先于allow)
	                    @WebInitParam(name="loginUsername",value="admin"),// 用户名
	                    @WebInitParam(name="loginPassword",value="123456"),// 密码
	                    @WebInitParam(name="resetEnable",value="false")// 禁用HTML页面上的“Reset All”功能
	           }
	)
	public class DruidStatViewServlet extends StatViewServlet{
	    privatestaticfinallongserialVersionUID = 1L;
	   
	}*/
	//配置filter
	/**
	 * druid过滤器.
	 * @author Administrator
	 *
	 */
	/*@WebFilter(filterName="druidWebStatFilter",urlPatterns="/*",
	    initParams={
	             @WebInitParam(name="exclusions",value="*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*")// 忽略资源
	     }
	)
	public class DruidStatFilter extends WebStatFilter{
	 
	}*/
	
	/**
	 * 注册一个StatViewServlet
	 * @return
	 */
	@Bean
	public ServletRegistrationBean DruidStatViewServlet(){
		ServletRegistrationBean servletRegistrationBean=new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
		//初始化参数 initParams
		//白名单
		servletRegistrationBean.addInitParameter("allow","127.0.0.1");
		servletRegistrationBean.addInitParameter("deny", "192.168.10.6");
		
		//登录查看信息的账户密码
		servletRegistrationBean.addInitParameter("loginUsername","admin");
		servletRegistrationBean.addInitParameter("loginPassword", "123456");
		//是否能够重置数据.
	    servletRegistrationBean.addInitParameter("resetEnable","false");
	    return servletRegistrationBean;
	}
	/**
	 * 注册一个：filterRegistrationBean
	 */
	@Bean
	public FilterRegistrationBean druidStatFilter(){
		FilterRegistrationBean filterRegistrationBean=new FilterRegistrationBean(new WebStatFilter());
		//添加过滤规则.
	    filterRegistrationBean.addUrlPatterns("/*");
	      
	       //添加不需要忽略的格式信息.
	    filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
	    return filterRegistrationBean;
	}
	
}
